﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LineDrawer : MonoBehaviour
{
    public static bool isShowGridLine = false;
    public Material lineMaterial;

    private void Awake()
    {
//        if (!lineMaterial)
//        {
//            lineMaterial = new Material(Shader.Find("Particles/Alpha Blended"));
//            lineMaterial.hideFlags = HideFlags.HideAndDontSave;
//            lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
//        }
    }

    private void Start()
    {
        DrawLine();
    }

    private void DrawLine()
    {
        if (isShowGridLine)
        {
            GL.PushMatrix();
            lineMaterial.SetPass(0);
            //如果报错的话，将这句话取消注释后，再试试
            GL.LoadPixelMatrix();
            GL.LoadOrtho();
            GL.Begin(GL.LINES);
            for (int i = 0; i < 10; i++)
            {
                GL.Color(Color.green);
                //横线
                GL.Vertex3(0, i / 10f, 0);
                GL.Vertex3(1, i / 10f, 0);
                //竖线
                GL.Vertex3(i / 10f, 0, 0);
                GL.Vertex3(i / 10f, 1, 0);
            }
            //        //斜线
            //        GL.Vertex3(0,0,0);
            //        GL.Vertex3 (1,1,0);

            GL.End();
            GL.PopMatrix();
        }

    }

    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            isShowGridLine = true;
        }

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            isShowGridLine = false;
        }

    }
}
